﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using System.IO;

namespace TTBox
{
    public partial class DispatchConfirmPanel : UserControl
    {

        public enum ConfirmPanelType { DispatchConfirm, MessageRead,JobRead,ReserveJobRead , MessageRecv, Error};

        public DispatchConfirmPanel()
        {
            InitializeComponent();
        }


        string m_strLastCMD = "";
        string m_strLastOrderID = "";
        ConfirmPanelType m_cpt = ConfirmPanelType.Error;
        public bool m_IsDispatchConfirm = false;
        int m_nMsgIndex = -1;
        DateTime dtMsgRing = System.DateTime.Now;

        public void initial(string arg_strCMD, ConfirmPanelType arg_Type, int arg_nIndex)
        {

            m_nMsgIndex = arg_nIndex;
            this.button_F4.Visible = false;

            m_IsDispatchConfirm = true;
            m_cpt = arg_Type;
            m_strLastCMD = arg_strCMD;

            switch (m_cpt)
            {
                case ConfirmPanelType.DispatchConfirm:
                    if (this.Visible) SoundManager.getInstance().playJobGot();
                    dtMsgRing = System.DateTime.Now.AddSeconds(StoreData.getInstance().JobRingSecs);

                    break;
                case ConfirmPanelType.MessageRecv:
                    if (this.Visible) SoundManager.getInstance().playMsgRecv();
                    dtMsgRing = System.DateTime.Now.AddSeconds(StoreData.getInstance().MsgRingSecs);

                    break;
            }
            
            this.label_NoExecute.Visible = false;
            LogManager.getInstance().Log("initial DCP:" + arg_Type.ToString());
            this.timer_Timeout.Enabled = true;
            this.button_DelMsg.Visible = false;


            switch (m_cpt)
            {
                case ConfirmPanelType.DispatchConfirm:
                    this.button_F4.Visible = false;
                    m_strLastOrderID = arg_strCMD.Substring(3, 15);
                    this.textBox_Msg.Text = arg_strCMD.Substring(19);
                    TrimMessageEnd();

                    LogManager.getInstance().Log("DispatchConfirm OrderID2:" + m_strLastOrderID);


                    //計算特殊按鈕分鐘數
                    switch (m_strLastCMD[3])
                    {
                        case '1':
                        case '5':
                        case '2':
                            if (Form_Main.Main.Screen_AskScreen.m_strLastOrderID == m_strLastOrderID)
                            {
                                Form_Main.Main.Screen_MainScreen.setRunJobInfo(m_strLastOrderID, arg_strCMD, Form_Main.Main.Screen_AskScreen.m_nLastMins);
                            }
                            else
                            {
                                Form_Main.Main.Screen_MainScreen.setRunJobInfo(m_strLastOrderID, arg_strCMD, StoreData.getInstance().StationJobMins);
                            }

                            break;
                        case '4':
                        case '3':

                            //預約任務, 不用處理
                            break;
                    }

                    //先送回已收到..
                    CmdTalker.getInstance().SendServer(Form_Main.Main.getCurrStatusChar() + "91" + m_strLastOrderID);

                    this.button_F1.Visible = false;

                    this.button_F2.Text = "了解";
                    this.button_F2.Visible = true;

                    break;
                case ConfirmPanelType.MessageRead:
                    this.button_DelMsg.Visible = true;
                    this.button_F4.Visible = false;
                    m_IsDispatchConfirm = false;

                    this.textBox_Msg.Text = arg_strCMD.Substring(15);
                    TrimMessageEnd();

                    this.button_F1.Text = "上一頁";
                    this.button_F1.Visible = true;

                    this.button_F2.Text = "主畫面";
                    this.button_F2.Visible = true;

                    break;
                case ConfirmPanelType.JobRead:
                    this.button_DelMsg.Visible = true;
                    if(StoreData.getInstance().IsBillEnable)
                        this.button_F4.Visible = true;
                    m_strLastOrderID = arg_strCMD.Substring(0, 15);

                    m_IsDispatchConfirm = false;

                    this.textBox_Msg.Text = arg_strCMD.Substring(16);
                    TrimMessageEnd();

                    this.button_F1.Text = "上一頁";
                    this.button_F1.Visible = true;

                    this.button_F2.Text = "主畫面";
                    this.button_F2.Visible = true;

                    break;

                case ConfirmPanelType.MessageRecv:
                    this.button_F4.Visible = false;
                    m_IsDispatchConfirm = false;

                    this.textBox_Msg.Text = arg_strCMD.Substring(15);
                    TrimMessageEnd();

                    this.button_F1.Visible = false;

                    this.button_F2.Text = "返回";
                    this.button_F2.Visible = true;

                    break;

                case ConfirmPanelType.ReserveJobRead:
                    this.button_F4.Visible = false;
                    m_IsDispatchConfirm = false;
                    m_strLastOrderID = arg_strCMD.Substring(3, 15);

                    string[] strTemp2 = arg_strCMD.Substring(19).Split('\t');
                    this.textBox_Msg.Text = strTemp2[0];


                    string s = Form_Main.Main.Screen_MainScreen.CurrStatus;
                    if (s == "0" || s == "1" || s == "5")
                    {

                        this.button_F1.Text = "前往";
                        this.button_F1.Visible = true;
                    }
                    else
                    {
                        this.label_NoExecute.Visible = true;
                        this.button_F1.Visible = false;
                    }

                    this.button_F2.Text = "主畫面";
                    this.button_F2.Visible = true;

                    break;


            }

        }

        /// <summary>
        /// 由於派遣地址尾端有經緯度, 因此要清除掉
        /// </summary>
        private void TrimMessageEnd()
        {
            int nIndex = this.textBox_Msg.Text.IndexOf('\t');
            if (nIndex != -1)
            {
                textBox_Msg.Text = textBox_Msg.Text.Split('\t')[0];
            }

        }

        private void button_F2_Click(object sender, EventArgs e)
        {
            switch (m_cpt)
            {
                case ConfirmPanelType.DispatchConfirm:
                    m_IsDispatchConfirm = false;
                    Form_Main.Main.ChangeToMainScreen();

                    switch (m_strLastCMD[3])
                    {
                        case '1':
                        case '5':
                        case '2':
                            Form_Main.Main.SetCurrStatus(TaxiStatus.Forward); //即時的case要變成前往
                            break;
                        case '4':
                        case '3':

                            //預約任務, 
                            break;
                    }
                    break;

                case ConfirmPanelType.MessageRead:
                    //註記訊息讀過

                    Form_Main.Main.ChangeToMainScreen();

                    break;
                case ConfirmPanelType.JobRead:
                    //註記訊息讀過

                    Form_Main.Main.ChangeToMainScreen();

                    break;
                case ConfirmPanelType.MessageRecv:


                    Form_Main.Main.ChangeToMainScreen();

                    //註記訊息讀過

                    break;


                case ConfirmPanelType.ReserveJobRead:
                    Form_Main.Main.ChangeToMainScreen();

                    break;

            }

        }


        private void timer_Timeout_Tick(object sender, EventArgs e)
        {
            //持續 Beep Beep 叫

            if (dtMsgRing > System.DateTime.Now)
            {
                switch (m_cpt)
                {
                    case ConfirmPanelType.DispatchConfirm:
                        if (this.Visible) SoundManager.getInstance().playJobGot();
                        break;
                    case ConfirmPanelType.MessageRecv:
                        if (this.Visible) SoundManager.getInstance().playMsgRecv();
                        break;
                }
            }

        }

        private void button_F1_Click(object sender, EventArgs e)
        {
            switch (m_cpt)
            {
                case ConfirmPanelType.DispatchConfirm:
                    break;

                case ConfirmPanelType.MessageRead:
                    //註記訊息讀過
                    this.Visible = false;

                    break;
                case ConfirmPanelType.JobRead:
                    //註記訊息讀過
                    this.Visible = false;

                    break;
                case ConfirmPanelType.ReserveJobRead:
                    //把預約任務設為執行中任務, 並變成前往狀態.
                    Form_Main.Main.Screen_MainScreen.setRunJobInfo(m_strLastOrderID, m_strLastCMD, -1);
                    int n = StoreData.getInstance().delReserveJob(m_strLastOrderID);
                    StoreData.getInstance().addDispatch("10" + m_strLastCMD);

                    Form_Main.Main.SetCurrStatus(TaxiStatus.Forward); //即時的case要變成前往
                    Form_Main.Main.ChangeToMainScreen();

                    break;

            }
        }

        private void button_F3_Click(object sender, EventArgs e)
        {

        }

        private void button_F4_Click(object sender, EventArgs e)
        {
            Form_Main.Main.ChangeToBillInputPanel(m_strLastOrderID);

        }

        private void button_Cancel_Click(object sender, EventArgs e)
        {

        }

        private void button_OKSend_Click(object sender, EventArgs e)
        {

        }

        private void button_DelMsg_Click(object sender, EventArgs e)
        {
            if (m_nMsgIndex != -1)
            {
                switch (m_cpt)
                {
                    case ConfirmPanelType.MessageRead:
                        //註記訊息讀過
                        StoreData.getInstance().delMsg(m_nMsgIndex);
                        Form_Main.Main.ChangeToMsg();

                        break;
                    case ConfirmPanelType.JobRead:
                        //註記訊息讀過
                        StoreData.getInstance().delDispatch(m_nMsgIndex);
                        Form_Main.Main.ChangeToDispatchMsg();

                        break;

                }
            }
        }


    }
}
